//
//  ChannelNum.m
//  DianNaoWang
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "TuShangCollectionViewCell.h"
#import "CFNetworkFromAFNetworking.h"
#import "TushangInfo.h"
#import "MJRefresh.h"
#import "EnjoyImage.h"
@implementation TuShangCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _page = 1;
        self.dataSourceArray = [NSMutableArray array];
        [self creatView];
        [self getdataArray];
    }
    return  self;
}

-(void)creatView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.tableFooterView = [[UIView alloc]init];
    _tableView.backgroundColor = BackgroundColor;
    [self.contentView addSubview:_tableView];
    
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataSourceArray removeAllObjects];
        _page = 1;
        [self getdataArray];
    }];
    
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page ++;
        [self getdataArray];
    }];
    
    _HUD = [MBProgressHUD showHUDAddedTo:self.contentView animated:YES];
    _HUD.labelText = @"正在加载";
    _HUD.detailsLabelText = @"请耐心等待";

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    EnjoyImage *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[EnjoyImage alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.info = _dataSourceArray[indexPath.row];
    return cell;
}
//图赏cell自动适应高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:contentTextSize, NSFontAttributeName, nil];
    CGRect rect = [[_dataSourceArray[indexPath.row] title] boundingRectWithSize:CGSizeMake(ScreenWidth - 20 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height + (40 + 160) * FitHeight;
    //return 230;
}

#pragma mark 点击让代理执行方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate pushAndPassId:[_dataSourceArray[indexPath.row] Id]];
}

-(void)getdataArray
{
    NSString *urlString = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/ipj/tushang.php?page=%ld&retina=1&vs=iph382", _page];
    [CFNetworkFromAFNetworking askObjectByURLString:urlString BodyString:nil WithObjectBlock:^(id object) {
        NSArray *array = [object objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            TushangInfo *info = [[TushangInfo alloc]init];
            [info setValuesForKeysWithDictionary:dic];
            [_dataSourceArray addObject:info];
        }
        [_tableView reloadData];
        _HUD.hidden = YES;
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
    }];
}

@end
